<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ChargeRecord extends Model{
    
    protected $table = 'charge_records';

    protected $fillable = [
        'order_number',
        'user_id',
        'admin_id',
        'discharge_admin_id',
        'discharge_flow',
        'device_id',
        'money',
        'open_id',
        'flow',
        //充值的流量 + 充值时水机的剩余流量 = 充值之后的剩余流量
        'residual_flow',
        'status',
        'remark',
    ];

    //等待支付通知
    const ORDER_STATUS_WAIT_PAYMENT = 0;
    //该支付已通知，充值完成
    const ORDER_STATUS_PAID = 1;
    const ORDER_STATUS_RECHARGE = 2;
    const ORDER_STATUS_DISCHARGE = 3;
    const ORDER_STATUS_DISCHARGED = 4;

    public static $_ORDER_STATUSES = [
        self::ORDER_STATUS_WAIT_PAYMENT => '等待支付通知',
        self::ORDER_STATUS_PAID => '支付',
        self::ORDER_STATUS_RECHARGE => '充值',
        self::ORDER_STATUS_DISCHARGE => '充值撤销中',
        self::ORDER_STATUS_DISCHARGED => '撤销',
    ];

    protected $appends = [
        'status_description'
    ];

    public function getStatusDescriptionAttribute(){
        if(array_key_exists('status', $this->attributes)){
            return self::$_ORDER_STATUSES[$this->attributes['status']];
        }
    }

    public function device()
    {
        return $this->belongsTo('App\Models\Device', 'device_id', 'id');
    }

    public function userInfo()
    {
        return $this->belongsTo('App\Models\UserInfo', 'user_id', 'user_id');
    }

    public function adminInfo()
    {
        return $this->belongsTo('App\Models\AdminInfo', 'admin_id', 'user_id');
    }

    public function dischargeAdmin()
    {
        return $this->belongsTo('App\Models\AdminInfo', 'discharge_admin_id', 'user_id');
    }

}
